package com.cn.qgx428.orderingmeal;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.adapter.OrderAdapter;
import com.cn.qgx428.orderingmeal.myClass.DimOrderInfo;
import com.cn.qgx428.orderingmeal.myClass.Food;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;
import com.cn.qgx428.orderingmeal.myClass.Order;
import com.cn.qgx428.orderingmeal.myClass.User;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

public class OrderActicity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "OrderActivity";

    private MyKeyInfo keyInfo;

    private ImageView back;

    private RecyclerView recyclerView;
    private OrderAdapter adapter;
    private List<DimOrderInfo> orderInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        Bmob.initialize(this,"1b925fe2ccbb402f4034a3e28c31ad93");

        init();
        getOrderData();
    }

    private void getOrderData() {
        User user = new User();
        user.setObjectId(keyInfo.getUserID());
        BmobQuery<Order> query = new BmobQuery<Order>();
        query.addWhereEqualTo("user", new BmobPointer(user));
        query.order("-createdAt");
        final List<Food> foods = new ArrayList<Food>();
        query.findObjects(new FindListener<Order>() {
            @Override
            public void done(final List<Order> list, BmobException e) {
                if (e == null){
                    //获取简略订单页面数据
                    for(int i=0 ; i<list.size() ; i++){
                        String [] foodIDS = list.get(i).getFoodsID().split(",");
                        String [] foodTotalPrice = list.get(i).getItemTotalPrice().split(",");
                        DimOrderInfo orderInfo = new DimOrderInfo();
                        orderInfos.add(orderInfo);
                        orderInfos.get(i).setTotalAmount(String.valueOf(foodIDS.length));
                        orderInfos.get(i).setTotalPrice(String.valueOf(list.get(i).getTotalPrice()));
                        orderInfos.get(i).setFoodName(foodIDS[0]);
                        orderInfos.get(i).setOrderNo(list.get(i).getObjectId());
                        orderInfos.get(i).setOrderTime(list.get(i).getCreatedAt());
                    }

                    //配置适配器
                    adapter = new OrderAdapter(OrderActicity.this ,orderInfos,R.layout.order_order_item_layout);
                    adapter.setOrderItemOnclickListener(new OrderAdapter.OnOrderItemOnclickListener() {
                        @Override
                        public void onClick(int position) {
                            Intent intent = new Intent(OrderActicity.this , OrderDetail.class);
                            intent.putExtra("userID",keyInfo.getUserID());
                            intent.putExtra("orderID",list.get(position).getObjectId());
                            startActivity(intent);
                        }
                    });
                    recyclerView.setAdapter(adapter);
                }
                else {
                    Toast.makeText(OrderActicity.this, "服务器错误", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "获取简略订单页面数据："+e.getErrorCode()+e.getErrorCode());
                }

            }
        });
    }

    private void init() {
        keyInfo = (MyKeyInfo) getIntent().getSerializableExtra("keyInfo");

        back = (ImageView) findViewById(R.id.order_back);
        back.setOnClickListener(this);

        recyclerView = (RecyclerView) findViewById(R.id.order_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        orderInfos = new ArrayList<DimOrderInfo>();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.order_back:
                finish();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
